package com.zlc.workflow.core.task.controller;


import com.zlc.common.common.entity.PageVo;
import com.zlc.common.core.process.model.vo.StoneProcessInstanceVo;
import com.zlc.common.core.user.mapper.MayanPlatUserAgentMapper;
import com.zlc.common.model.process.instance.FlowInstanceVo;
import com.zlc.common.model.process.instance.QueryInstanceParam;
import com.zlc.common.model.process.task.FlowTaskVo;
import com.zlc.common.model.process.task.QueryTaskParam;
import com.zlc.common.util.UserHelper;
import com.zlc.workflow.core.task.model.AddTaskActorParam;
import com.zlc.workflow.core.task.model.ApproveHistTaskVo;
import com.zlc.workflow.core.task.model.TransferTaskParam;
import com.zlc.workflow.core.task.model.WorkflowTraceVo;
import com.zlc.workflow.core.task.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Api(tags = "任务处理相关api")
@RestController
@Slf4j
@RequestMapping("/api/process/task")
public class TaskController {

    @Autowired
    private TaskService taskService;
    @Autowired
    private MayanPlatUserAgentMapper mayanPlatUserAgentMapper;

    /**
     * 查询流程跟踪数据
     */
    @ApiOperation(value = "查询流程任务详情")
    @GetMapping(value = "/getTraceList")
    public WorkflowTraceVo getTraceList(@RequestParam(value = "processId") String processId,
                                        @RequestParam(value = "processInstanceId") String processInstanceId){
        return taskService.getTraceList(processId,processInstanceId);
    }

    /**
     * 查询待办任务列表
     */
    @ApiOperation(value = "查询待办任务列表")
    @GetMapping(value = "/queryTaskTodo")
    public PageVo<FlowTaskVo> queryTaskTodo(QueryTaskParam param){
        List<String> agentUsers = getAccountList();
        param.setAccountList(agentUsers);
        return taskService.queryTaskTodo(param);
    }

    private List<String> getAccountList(){
        try {
            List<String> agentUsers = mayanPlatUserAgentMapper.queryAgentUserAccount(UserHelper.getNowAccount(), new Date());
            agentUsers.add(UserHelper.getNowAccount());
            return agentUsers;
        }catch (Exception e){
            return Collections.singletonList(UserHelper.getNowAccount());
        }
    }

    /**
     * 查询流程列表
     */
    @ApiOperation(value = "查询流程列表")
    @GetMapping(value = "/queryInstancePage")
    public PageVo<FlowInstanceVo> queryInstancePage(QueryInstanceParam param){
        return taskService.queryInstancePage(param);
    }

    /**
     * 查询已办任务列表
     */
    @ApiOperation(value = "查询已办任务列表")
    @GetMapping(value = "/queryTaskHistory")
    public PageVo<FlowTaskVo> queryTaskHistory(QueryTaskParam param){
        List<String> agentUsers = getAccountList();
        param.setAccountList(agentUsers);
        return taskService.queryTaskHistory(param);
    }

    /**
     * 查询指定流程的历史审批人
     */
    @ApiOperation(value = "查询指定流程的历史审批人")
    @GetMapping(value = "/getTaskApprovalHist")
    public List<ApproveHistTaskVo> getTaskApprovalHist(@RequestParam(value = "processInstanceId") String processInstanceId){
        return taskService.getTaskApprovalHist(processInstanceId);
    }

    /**
     * 为节点任务添加审批人  加签
     */
    @ApiOperation(value = "为节点任务添加审批人 加签")
    @PostMapping(value = "/addTaskActor")
    public void addTaskActor(@RequestBody AddTaskActorParam param){
        taskService.addTaskActor(param);
    }

    /**
     * 转办任务
     */
    @ApiOperation(value = "转办任务")
    @PostMapping(value = "/transferTask")
    public void transferTask(@RequestBody TransferTaskParam param){
        taskService.transferTask(param);
    }
}
